package com.adee.gulimall.product.app;

import com.adee.gulimall.common.utils.PageUtils;
import com.adee.gulimall.util.utils.R;
import com.adee.gulimall.common.validator.group.AddGroup;
import com.adee.gulimall.common.validator.group.UpdateGroup;
import com.adee.gulimall.common.validator.group.UpdateShowStatus;
import com.adee.gulimall.product.entity.BrandEntity;
import com.adee.gulimall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 品牌
 *
 * @author adee
 * @email 609980188@qq.com
 * @date 2022-08-19 16:28:49
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    // @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     * @Valid 告诉springmvc该字段需要校验，BindingResult紧挨着@Valid字段之后，表示校验结果
     * 改成使用ControllerAdvice统一拦截异常
     * @Validated 标注检验分组
     * controller中指定分组了，则entity中标注分组的字段规则生效，未标注分组的字段规则都不生效
     * 反之，controller中没指定分组，则entity中没标注分组的规则生效，标注了分组的规则都不生效
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:brand:save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand){
//        if(result.hasErrors()) {
//            Map<String, String> map = new HashMap<>();
//            result.getFieldErrors().forEach(err -> {
//                map.put(err.getField(), err.getDefaultMessage());
//            });
//            return R.error(400, "数据校验不合法").put("data", map);
//        }
		brandService.save(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand){
        brandService.updateDetails(brand);

        return R.ok();
    }

    /**
     * 更新状态
     */
    @RequestMapping("/updateShowStatus")
    // @RequiresPermissions("product:brand:update")
    public R updateShowStatus(@Validated({UpdateShowStatus.class}) @RequestBody BrandEntity brand){
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
